<?php
/**
 * Created by PhpStorm.
 * User: lgy
 * Date: 2018/6/30
 * Time: 14:57
 */

namespace app\admin\model;
use think\Db;
use think\Model;

class Work extends Model
{
	//获取全部事务
	public static function getAllWork($params){
		$limit = 10;
		$params['page'] = isset($params['page'])?$params['page']:1;
		$start = $params['page'] * $limit - 10;
		$condition = [
			'w.status'=>1
		];
		$field = [
			'w.id AS word_id,w.title,w.content,w.initiator_id,w.receiver_id,w.style,w.privacy,w.create_time,w.update_time,w.status',
			'i.img_url',
			'l.id AS list_id,l.admin_id AS list_admin_id,l.content AS list_content,l.style AS list_style',
			'ai.nickname AS initiator_name',
			'ar.nickname AS receiver_name',
			'al.nickname AS list_name'
		];
		$join = [
			['dbkj_work_img i','i.work_id=w.id','LEFT'],
			['dbkj_work_list l','l.work_id=w.id','LEFT'],
			['dbkj_admin ai','ai.id=w.initiator_id','LEFT'],
			['dbkj_admin ar','ar.id=w.receiver_id','LEFT'],
			['dbkj_admin al','al.id=l.admin_id','LEFT'],
		];
		$result = Db::table('dbkj_work')->alias('w')->field($field)->join($join)->where($condition)->limit($start,$limit)->select();
		return $result;
	}
	//添加事务
	public static function addWork($params){
		$insert = [
			'title'=>$params['title'],
			'content'=>$params['content'],
			'initiator_id'=>$params['initiator_id'],
			'receiver_id'=>$params['receiver_id'],
			'style'=>$params['style'],
			'privacy'=>$params['privacy'],
			'create_time'=>time(),
			'update_time'=>time(),
			'status'=>1
		];
		$result = Db::table('dbkj_work')->insert($insert);
//		添加图片
//		$work_id = Db::table('dbkj_work')->getLastInsID();
//		$params['work_id'] = $work_id;
// 		self::addWorkImg($params);
		return $result;
	}
	//插入图片
	public static function addWorkImg($params){
		$insert = [
			'word_id'=>$params['work_id'],
			'img_url'=>$params['img_url'],
			'create_time'=>time(),
			'status'=>1
		];
		$result = Db::table('dbkj_work_img')->insert($insert);
		return $result;
	}
}